package iha.bachelor.smo.aba.rah.easyconnect_v3.adapter;

import iha.bachelor.smo.aba.rah.easyconnect_v3.MainActivity;
import iha.bachelor.smo.aba.rah.easyconnect_v3.R;
import iha.bachelor.smo.aba.rah.easyconnect_v3.contentprovider.FileHandler;
import iha.bachelor.smo.aba.rah.easyconnect_v3.model.ECRU;
import iha.bachelor.smo.aba.rah.easyconnect_v3.model.ModuleInfo;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
 
public class ExpandableListAdapter extends BaseExpandableListAdapter {
	private Activity context;
    private Map<ECRU, List<String>> moduleNameCollections;
    private List<ECRU> roomUnitNames;
    private static String LOG_TAG = "ExpandableListAdapter";
 
    public ExpandableListAdapter(Activity context, List<ECRU> roomUnits, Map<ECRU, List<String>> moduleCollections) {
        this.context = context;
        this.moduleNameCollections = moduleCollections;
        this.roomUnitNames = roomUnits;
    }
 
    public Object getChild(int groupPosition, int childPosition) {
        return moduleNameCollections.get(roomUnitNames.get(groupPosition)).get(childPosition);
    }
 
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
     
     
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final String module = (String) getChild(groupPosition, childPosition);
        
        ModuleInfo testMI = ModuleInfoParser.ByteToInfo(
        		FileHandler.ReadBytesFromFile(context,
        				MainActivity.CurrentProfileName,
        				FileHandler.MODULE_DIR,
        				module+".BLE"
        				));
        // get name of module instead and set as string.
        
        
        LayoutInflater inflater = context.getLayoutInflater();
         
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.module_item, null);
        }
         
        TextView item = (TextView) convertView.findViewById(R.id.laptop);
        
        ImageView delete = (ImageView) convertView.findViewById(R.id.delete);
        delete.setOnClickListener(new OnClickListener() {
             
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setMessage("Do you want to remove?");
                builder.setCancelable(false);
                builder.setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                List<String> child = 
                                    moduleNameCollections.get(roomUnitNames.get(groupPosition));
                                child.remove(childPosition);
                                notifyDataSetChanged();
                            }
                        });
                builder.setNegativeButton("No",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }
        });
         
        try {
			item.setText(new String(testMI.Name.Value,"UTF-8"));
			Log.i(LOG_TAG, "Text is:  "+item.getText());
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return convertView;
    }
 
    public int getChildrenCount(int groupPosition) {
        return moduleNameCollections.get(roomUnitNames.get(groupPosition)).size();
    }
 
    public Object getGroup(int groupPosition) {
        return roomUnitNames.get(groupPosition);
    }
 
    public int getGroupCount() {
        return roomUnitNames.size();
    }
 
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
 
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        ECRU RoomUnit = (ECRU) getGroup(groupPosition);
        String ecruName = RoomUnit.Name;
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.room_unit_item,
                    null);
        }
        TextView item = (TextView) convertView.findViewById(R.id.laptop);
        item.setTypeface(null, Typeface.BOLD);
        item.setText(ecruName);
        return convertView;
    }
 
    public boolean hasStableIds() {
        return true;
    }
 
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}